<template>
  <div class="app-container">
    <basics-form
      :minWidth="'300px'"
      ref="basicsForm"
      :actionLoading="loading"
      @actionBtnsClick="actionBtnsClick"
      @submit="search"
    />
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      border
      stripe
      style="width: 1000px"
    >
      <el-table-column prop="deptName" label="序号" width="100px">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="deptName" label="名称"> </el-table-column>
      <el-table-column prop="createTime" label="创建时间"> </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="190px"
        fixed="right"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="showDetails(scope.row)"
            >修改</el-button
          >
          <el-popconfirm
            style="margin: 0 10px"
            title="确定删除吗？"
            @confirm="del(scope.row.deptId)"
          >
            <el-button
              slot="reference"
              size="mini"
              type="text"
              icon="el-icon-delete"
              >删除</el-button
            >
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="width: 1000px">
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="$refs.basicsForm.formValid()"
      />
    </div>

    <el-dialog
      :title="item ? '修改' : '新增'"
      :visible.sync="openView"
      width="700px"
      append-to-body
    >
      <basics-form
        style="margin-left: 100px"
        ref="basicsAddForm"
        :titleAligin="'right'"
        :titleWidth="'150px'"
        :actionLoading="loading"
        hiddenMustIcon
        @actionBtnsClick="addAction"
        @submit="addSubmit"
      />
    </el-dialog>
  </div>
</template>

<script>
import BasicsForm from "@/components/BasicsForm";
import {
  departmentList,
  saveDepartment,
  delDepartment,
} from "@/api/manbing/hospital";
export default {
  name: "Department",
  components: { BasicsForm },
  data() {
    return {
      loading: false,
      tableLoading: false,
      openView: false, //新增医院弹出
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      provinces: null, //省市区列表
      levels: null, //医院等级列表
      item: null, //操作的医院
    };
  },
  created() {
    this.$nextTick(() => {
      this.initRules();
      this.getList();
    });
  },
  methods: {
    //顶部搜索组件
    initRules() {
      let rules = [
        {
          title: "名称",
          key: "keyWord",
          skip: true,
        },
        {
          type: "actionBtns",
          selectList: [
            { name: "搜索", value: "primary" },
            { name: "新增", value: "success" },
          ],
          ruleWidth: "auto",
        },
      ];
      this.$refs.basicsForm.initForm(rules);
    },
    //顶部按钮点击
    actionBtnsClick(index) {
      if (index == 1) {
        this.id = "";
        this.openView = true;
        this.item = null;
        this.$nextTick(() => {
          this.initAddRules();
        });
      } else {
        this.queryParams.pageNum = 1;
        this.$refs.basicsForm.formValid();
      }
    },
    //搜索
    search(param) {
      if (!param) {
        return;
      }
      for (let key in param) {
        this.queryParams[key] = param[key];
      }
      this.getList();
    },
    //列表数据
    getList() {
      this.loading = true;
      this.tableLoading = true;
      departmentList(this.queryParams)
        .then((res) => {
          if (res.code == 200) {
            this.tableData = res.rows;
            this.total = res.total;
          }
        })
        .finally(() => {
          this.loading = false;
          this.tableLoading = false;
        });
    },
    //详情和编辑
    showDetails(item) {
      this.openView = true;
      this.item = item;
      this.$nextTick(() => {
        this.initAddRules();
      });
    },
    //构建新增/修改表单
    initAddRules() {
      let item = this.item ? this.item : {};
      let rules = [
        {
          title: "名称",
          key: "deptName",
          value: item.deptName || "",
        },
        {
          type: "actionBtns",
          selectList: [
            { name: "取消", value: "" },
            { name: "提交", value: "primary" },
          ],
          btnsAligin: "center",
          ruleWidth: "503px",
        },
      ];
      this.$refs.basicsAddForm.initForm(rules);
    },
    //新增表单的按钮点击事件
    addAction(index) {
      if (index) {
        this.$refs.basicsAddForm.formValid();
      } else {
        this.openView = false;
      }
    },
    //新增，发送请求
    addSubmit(param) {
      if (!param) {
        return;
      }
      if (this.item) {
        param.deptId = this.item.deptId;
      }
      this.loading = true;
      saveDepartment(param)
        .then((res) => {
          if (res.code == 200) {
            this.$modal.msgSuccess(this.item ? "修改成功" : "新增成功");
            this.openView = false;
            this.getList();
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    //删除
    del(deptId) {
      this.loading = true;
      this.tableLoading = true;
      delDepartment({ deptId })
        .then((res) => {
          if (res.code == 200) {
            this.$modal.msgSuccess("删除成功");
            this.getList();
          }
        })
        .finally(() => {
          this.loading = false;
          this.tableLoading = false;
        });
    },
  },
};
</script>
<style scoped lang="scss">
</style>

